<template>
    <div style="display: inline;">
        <!-- <el-button :type="buttonType" icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button> -->
        <el-drawer :visible.sync="show" :close-on-press-escape="false" :title="`${form.id ? '修改' : '新增'}活动`"
            custom-class="employee-store-drawer" modal-append-to-body append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-position="right" label-width="130">
                <el-form-item label="活动名称" prop="title">
                    <el-input v-model="form.title" placeholder="请输入活动名称" />
                </el-form-item>
                <el-form-item label="活动总奖励" prop="activity_reward">
                    <el-input v-model="form.activity_reward" placeholder="请输入活动总奖励" />
                </el-form-item>
                <el-form-item label="活动图" prop="img">
                    <ml-upload-image :url.sync="form.img" />
                </el-form-item>
                <el-form-item label="起止时间" prop="start_time">
                    <el-date-picker v-model="form.start_time" type="datetimerange" range-separator="至"
                        start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd HH:mm:ss">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="排序" prop="sort">
                    <el-input v-model="form.sort" type="number" placeholder="请输入排序" />
                </el-form-item>
                <el-form-item label="状态">
                    <el-switch v-model="form.status" :active-value="1" :inactive-value="2" @change="updatastatus">
                    </el-switch>
                </el-form-item>
                <el-form-item label="活动说明" prop="desc">
                    <textQuillEditor v-model='form.desc' style="height: 500px;"></textQuillEditor>
                </el-form-item>
                <el-form-item label="规则说明" prop="rule_desc">
                    <textQuillEditor v-model='form.rule_desc' style="height: 500px;"></textQuillEditor>
                </el-form-item>
                <el-form-item label="平台说明" prop="platform_desc">
                    <textQuillEditor v-model='form.platform_desc' style="height: 500px;"></textQuillEditor>

                </el-form-item>
            </el-form>
            <div class="footer">
                <el-button @click="show = false">取 消</el-button>
                <el-button @click="form.id ? handleUpdata() : handleSubmit()">确 定</el-button>
            </div>
        </el-drawer>
    </div>
</template>
<script>
const Model = {
    id: "",	//true	int		id
    title: "",	//true	string		名称
    img: "",	//true	string		封面图
    start_time: "",	//true	string		开始时间
    end_time: "",	//true	string		结束时间
    activity_reward: "",	//true	int		活动总奖励
    desc: "",	//true	string		活动说明
    rule_desc: "",	//true	string		规则说明
    platform_desc: "",	//true	string		平台说明
    sort: "",	//true	int		排序位置
    status: "",	//true	int		状态 1启用 2禁用
}
import MlUploadImage from '@/components/MlUploadImg/single.vue'
import textQuillEditor from '@/components/textQuillEditor/index.vue'

export default {
    name: 'addNest',
    components: { MlUploadImage, textQuillEditor },
    props: {
        buttonType: {
            type: String,
            default() {
                return 'primary'
            }
        }
    },
    data() {
        return {
            rules: {
                title: [
                    { required: true, message: '请输入活动名称', trigger: 'blur' },
                ],
                img: [
                    { required: true, message: '请上传活动图', trigger: 'blur' },
                ],
                sort: [
                    { required: true, message: '请输入排序', trigger: 'blur' },
                ],
                activity_reward: [
                    { required: true, message: '请输入活动总奖励', trigger: 'blur' },
                ],
                start_time: [
                    { required: true, message: '请选择起止时间', trigger: 'blur' },
                ],
                // type: [
                //     { required: true, message: '请选择跳转类型', trigger: 'blur' },
                // ],
            },
            show: false,
            form: { ...{}, ...Model },
        }
    },
    methods: {
        updatastatus(e) {
            this.form.status = e
        },
        handleEdit: function (data) {
            let obj = JSON.parse(JSON.stringify(data))
            obj.start_time = [obj.start_time, obj.end_time]
            this.form = { ...{}, ...obj }
            this.show = true
        },
        handleUpdata: function () {
            this.$refs.form.validate(async (valid) => {
                if (valid) {
                    let data = JSON.parse(JSON.stringify(this.form))
                    data.start_time = this.form.start_time[0]
                    data.end_time = this.form.start_time[1]
                    let res = await this.$request({
                        url: '/admin/work.activity/edit',
                        method: 'post',
                        data: data
                    })
                    if (res.code == 200) {
                        this.show = false
                        this.$message.success('操作成功')
                        this.$emit('success')
                    }

                } else {
                    return false
                }
            })
        },
        handleAdd: function () {
            this.form = { ...{}, ...Model }
            this.show = true
        },
        handleSubmit: function () {
            this.$refs.form.validate(async (valid) => {
                if (valid) {
                    let res = await this.$request({
                        url: '/admin/work.activity/add',
                        method: 'post',
                        data: this.form
                    })
                    if (res.code == 200) {
                        this.show = false
                        this.$message.success('操作成功')
                        this.$emit('success')
                    }

                } else {
                    return false
                }
            })
        }
    }
}

</script>
<style lang="scss">
.employee-store-drawer {
    width: 500px !important;

    .el-drawer__title {
        border-top-left-radius: 10px !important;
    }

    .el-drawer__header {
        font-size: 16px !important;
        color: #333333 !important;
        font-weight: 700;
        text-align: center;
    }

    .el-drawer__body {
        border-bottom-left-radius: 10px !important;
        padding: 0 20px;
        overflow: scroll !important;
        padding-bottom: 50px;
    }

    .footer {
        position: absolute;
        width: 100%;
        bottom: 0;
        right: 0;
        height: 66px;
        opacity: 1;
        background: #fff;
        box-shadow: 0 -3px 6px 0 rgba(0, 0, 0, .04), 0 2px 4px 0 rgba(0, 0, 0, .5);
        display: flex;
        align-items: center;
        text-align: center;
        justify-content: center;
        z-index: 9999;
    }

    .el-form-item {
        display: block;
        margin-right: 0;
        margin-bottom: 24px !important;
    }

    .el-form-item__label {
        width: 150px;
    }

    .el-form-item__content {
        width: calc(100% - 204px);
        display: inline-block;
    }

}
</style>